<?php
declare (strict_types=1);

namespace app\user\logic\user;

use app\common\exception\LogicException;
use app\user\logic\teacher\Teacher;
use app\user\model\user\IncomeFlow as IncomeFlowModel;
use think\db\exception\DbException;

/**
 * 账户余额流水接口类
 */
class IncomeFlow extends IncomeFlowModel
{

    /**
     * 用户收入明细
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-05-20
     *
     * @throws DbException
     */
    public function flow($param): array
    {
        $time = $param['time'] ?? 'all';
        $type = $param['type'] ?? 'all';
        // 讲师判断
        $is_teacher = app(Teacher::class)->isTeacher();
        // 条件
        $map[] = ['user_id', '=', request()->mid];
        if ($time == 'month') $map[] = ['create_time', '>', strtotime(date("Y/m") . '/1')];

        if ($type == 'teacher' && $is_teacher) {
            $map[] = ['alter_type', '=', 52];
        } else if ($type == 'user') {
            $map[] = ['alter_type', 'IN', '54,55,56'];
        }
        // 明细
        $data = $this->field('alter_type,num,create_time,note')->where($map)->order('create_time', 'DESC')->paginate($this->getListRows());

        // 【0：扣除；1：增加；2：冻结；3：解冻；4：冻结扣除；5：分成收入； 6：分销收入；7：分享收入；8：收入提现；9：提现失败退回；】【51：机构分成；52：讲师分成；53：挂载分成；54：分享者分成；55：一级分销分成；56：二级分销分成；】
        // 收入总额
        if ($time == 'all' && $type == 'all') {
            $total_income = app(Income::class)->userTotalIncome();
        } else {
            $map[]        = ['alter_type', 'NOT IN', '8,9'];
            $total_income = $this->where($map)->sum('num');
        }

        return ['is_teacher' => $is_teacher, 'total_income' => $total_income, 'flow' => $data];
    }

    /**
     * 流水记录
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-05-27
     */
    public function updateFlow($uid, $alter_type, $num, $income, $rel_type, $rel_pk, $note = '')
    {
        $data = [
            'user_id'    => $uid,
            'alter_type' => $alter_type,
            'num'        => $num,
            'income'     => $income,
            'rel_type'   => $rel_type,
            'rel_pk'     => $rel_pk,
            'note'       => $note,
        ];

        $add = $this::create($data);
        if ($add->isEmpty()) throw new LogicException('流水添加失败');
        // 流水添加后处理事件
        // Event::subscribe(IncomeSubscribe::class);
        // Event::trigger('IncomeUpdate', $data);

        return $add;

    }

    /**
     * 推广课程带来的收益
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2021-05-19
     */
    public function courseIncome()
    {
        $mid  = request()->mid;
        return app(SplitCourse::class)->alias('s')->join('course c', "s.share_user_id=$mid AND s.status=1 AND s.course_id=c.id")->field('s.course_id,c.title,c.course_type,c.cover,FORMAT(sum(s.share_user_sum), 2)+0 as sum')->with(['course_cover', 'course_type_text'])->group('s.course_id')->paginate($this->getListRows());
    }

    /**
     * 推广用户带来的收益
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2021-05-19
     */
    public function userIncome()
    {
        $mid  = request()->mid;
        return app(SplitFlow::class)->alias('f')->join('user_split_course c', "f.rel_id=c.id AND f.user_id=$mid AND f.status=1 AND (f.type=55 OR f.type=56)")->join('user u', "u.id=c.user_id")->field('c.user_id,FORMAT(sum(f.num), 2)+0 as sum,u.avatar,u.nick_name')->group('c.user_id')->with(['user_avatar'])->paginate($this->getListRows());
    }

}
